package com.antcom.antread.antreaddatacrawl.himalayaData.controller;

import com.alibaba.fastjson.JSONObject;
import com.antcom.antread.antreaddatacrawl.himalayaData.entity.BookInfo;
import com.antcom.antread.antreaddatacrawl.himalayaData.service.BookInfoService;
import com.antcom.antread.common.core.baseRest.controller.BaseRestController;
import com.antcom.antread.common.core.baseRest.domain.GeneralSet;
import com.antcom.antread.common.log.annotation.Log;
import com.antcom.antread.common.log.enums.FunctionType;
import com.antcom.antread.common.log.enums.OperatorType;
import com.antcom.antread.common.log.annotation.Log;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


/**
 * 喜马拉雅:controller
 *
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/11/28 23:42
 */
@ApiOperation("himalaya爬取接口")
@RestController
@RequestMapping("himalaya")
public class DataCrawlController extends BaseRestController {

    @Resource
    private BookInfoService bookInfoService;


    @PostMapping("downBook")
    public GeneralSet downBookInfo() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        String bookName = parseObject.getString("bookName");
        String condition = parseObject.getString("condition");
        String page = parseObject.getString("page");
        try {
            bookInfoService.downBookInfo(bookName, condition, page);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @PostMapping("downChapterInfo")
    public GeneralSet downChapterInfo() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        String bookId = parseObject.getString("bookId");
        bookInfoService.downChapterInfo(bookId);
        return success();
    }

    @PostMapping("downBcMiddle")
    public GeneralSet downBcMiddle() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        String bookId = parseObject.getString("bookId");
        String sign = parseObject.getString("sign");
        bookInfoService.downBcMiddle(bookId, sign);
        return success();
    }

    @PostMapping("downAudio")
    public GeneralSet downAudio() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        Integer start = parseObject.getInteger("start");
        Integer end = parseObject.getInteger("end");
        String sign = parseObject.getString("sign");
        bookInfoService.downAudio(sign, start, end);
        return success();
    }

    @PostMapping("findBook")
    public GeneralSet findBook() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        String id = parseObject.getString("id");
        Optional<BookInfo> book = bookInfoService.findBook(id);
        return success(book);
    }

    /**
     * 用户搜索接口调用
     *
     * @return
     */
    @PostMapping("searchBook")
    public GeneralSet searchBook(@RequestBody String bookName) {
        try {
            List<BookInfo> book = bookInfoService.getBook(bookName);
            return success(book);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @PostMapping("downLocalUrl")
    public GeneralSet downLocalUrl() {
        String param = getParam(request);
        JSONObject parseObject = JSONObject.parseObject(param);
        String filePath = parseObject.getString("filePath");
        String dirPath = parseObject.getString("dirPath");
        String sign = parseObject.getString("sign");
        try {
            bookInfoService.downLocalUrl(filePath, dirPath, sign);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
